package com.homework.dao;

import com.homework.common.StringUtils;
import com.homework.model.User;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Date;
import java.sql.ResultSet;

/**
 * @desc:
 * @author: huan_nian
 * @date:
 */

@Repository
public class UserDao {
    @Autowired
    private Connection conn ;

    public int insert(User user) {
        /**
         * todo: 写出对应的sql
         */
        String sql = "INSERT INTO `user` (user_id,username,`password`,salt,nickname,email,phone,sex,age,img,`status`,create_time,update_time,last_login_time)" +
                "VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?)" ;
        System.out.println();
        int res = 0;
        try (
                PreparedStatement ps =  conn.prepareStatement(sql) ;
                ){
            ps.setString(1,user.getUserId());
            ps.setString(2,user.getUsername());
            ps.setString(3, StringUtils.md5Encoder(user.getPassword(),user.getSalt()));
            ps.setString(4,user.getSalt());
            ps.setString(5,user.getNickname());
            ps.setString(6,user.getEmail());
            ps.setString(7,user.getPhone());
            ps.setString(8,user.getSex());
            ps.setLong(9,user.getAge());
            ps.setString(10,user.getImg());
            ps.setString(11,user.getStatus());
            ps.setDate(12, new Date(new java.util.Date().getTime()));
            ps.setDate(13,new Date(new java.util.Date().getTime()));
            ps.setDate(14,new Date(new java.util.Date().getTime()));

            res = ps.executeUpdate();

            ps.close();


        } catch (Exception e) {
            /**
             * todo: 异常输出
             */
            e.printStackTrace();
        }
        return res ;
    }

    /**
     * todo: 实现登录
     * @param name
     * @param password
     * @return
     */
    public User login(String name, String password) {
         String sql = "SELECT\n" +
                 "t.id,\n" +
                 "t.user_id,\n" +
                 "t.username,\n" +
                 "t.`password`,\n" +
                 "t.salt,\n" +
                 "t.nickname,\n" +
                 "t.email,\n" +
                 "t.phone,\n" +
                 "t.sex,\n" +
                 "t.age,\n" +
                 "t.img,\n" +
                 "t.`status`,\n" +
                 "t.create_time,\n" +
                 "t.update_time,\n" +
                 "t.last_login_time\n" +
                 "from `user` t\n" +
                 "where t.username=?";
         User user = new User();
         try {
             PreparedStatement ps = conn.prepareStatement(sql);
             ps.setString(1,name);
             ResultSet res = ps.executeQuery();

             //读取数据
             while(res.next()){
                 user.setId(res.getLong(1));
                 user.setUserId(res.getString(2));
                 user.setUsername(res.getString(3));
                 user.setPassword(res.getString(4));
                 user.setSalt(res.getString(5));
                 user.setNickname(res.getString(6));
                 user.setEmail(res.getString(7));
                 user.setPhone(res.getString(8));
                 user.setSex(res.getString(9));
                 user.setAge(res.getLong(10));
                 user.setImg(res.getString(11));
                 user.setStatus(res.getString(12));
                 user.setCreateTime(res.getDate(13));
                 user.setUpdateTime(res.getDate(14));
                 user.setLastLoginTime(res.getDate(15));
             }
             res.close();
             ps.close();


         }catch (Exception e){
             e.printStackTrace();
         }
         //用户名验证
         try {
             String username = user.getUsername();
            if(username.equals("")||username==null){
                System.out.println("用户名出错");
                user=null;
            }
         }catch (Exception e){
             e.printStackTrace();
         }

         //密码验证
         try {
             String Md5Password = StringUtils.md5Encoder(password,user.getSalt());
             if(Md5Password.equals(user.getPassword())){
                 System.out.println("登陆成功");
             }else {
                 System.out.println("密码出错");
                 user=null;
             }
         }catch (Exception e){
             e.printStackTrace();
         }

         //todo：登录时间修改
         return user;
    }

    /**
     * todo: 统计所有的用户数
     * @return
     */
    public int countAllUsers() {

        String sql = "select count(*) from user";
        int count = 0;
        try {
            PreparedStatement ps = conn.prepareStatement(sql);

            ResultSet res = ps.executeQuery();
            while (res.next()){
                count = res.getInt(1);
            }

            res.close();
            ps.close();

        }catch (Exception e){
            e.printStackTrace();
        }
        return count ;
    }

}
